﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using ZMHConsoleApp2.CalcSR;

namespace ZMHConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                /************************************************
                //Step 1: Create an instance of the WCF proxy.
                CalculatorClient client = new CalculatorClient();

                // Step 2: Call the service operations.
                // Call the Add service operation.
                double value1 = 100.00D;
                double value2 = 15.99D;
                double result = client.DoubleAdd(value1, value2);
                //client.DoubleAddAsync(value1, value2);
                Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
                Console.WriteLine("Add({0},{1}) = {2}", 2,4,client.IntAdd(2,4));

                // Call the Subtract service operation.
                value1 = 145.00D;
                value2 = 76.54D;
                result = client.Subtract(value1, value2);
                Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);

                // Call the Multiply service operation.
                value1 = 9.00D;
                value2 = 81.25D;
                result = client.Multiply(value1, value2);
                Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);

                // Call the Divide service operation.
                value1 = 22.00D;
                value2 = 7.00D;
                result = client.Divide(value1, value2);
                Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);

                //Step 3: Closing the client gracefully closes the connection and cleans up resources.
                client.Close();
                /************************************************
                DCSVC.WCFWithDataContractSVCClient clt = new DCSVC.WCFWithDataContractSVCClient();
                foreach (var item in clt.GetStuList()) Console.WriteLine("{0}---{1}", item.Name, item.Age);
                Console.WriteLine("===================================================");
                foreach (var item in clt.GetCarInfo()) Console.WriteLine("{0}---{1}", item.Name, item.Price);
                Console.WriteLine("===================================================");
                foreach (var item in clt.GetDicInfo())
                {
                    Console.WriteLine("Num:{0}--Name:{1}--Age:{2}--Count:{3}", item.Key, item.Value.Name, item.Value.Age, item.Value.DicScore.Count);
                    foreach (var sItem in item.Value.DicScore) Console.WriteLine("|-----Index:{0}--Score:{1}", sItem.Key, sItem.Value);
                }
                /************************************************
                SessionSVC.WCFSessionSVCClient proxy = new SessionSVC.WCFSessionSVCClient();
                proxy.MyMethod();
                Console.WriteLine(proxy.InnerChannel.SessionId);
                proxy.MyMethod();
                Console.WriteLine(proxy.InnerChannel.SessionId);
                proxy.Close();

                /************************************************
                MySingletonSVC.MySingletonClient proxy2 = new MySingletonSVC.MySingletonClient();               
                proxy2.MyMethod();
                proxy2.Close(); 

                MySingletonSVC.MySingletonClient proxy3 = new MySingletonSVC.MySingletonClient();
                proxy3.MyOtherMethod();
                proxy3.Close();
                Console.ReadLine();
                /************************************************
                MSton.MySingletonClient proxy4 = new MSton.MySingletonClient();
                proxy4.MyOtherMethod();
                Console.WriteLine("Done！:{0}",proxy4.GetCount());
                proxy4.Close();
                Console.ReadLine();
                /************************************************
                MDC.IMyDuplexContractCallback callback = new BackCall4Svc();
                InstanceContext context = new InstanceContext(callback);
                /************************************************
                MDC.MyDuplexContractClient myDuplex = new MDC.MyDuplexContractClient(context);
                myDuplex.DoWork();
                Console.ReadLine();
                myDuplex.Close(); 
                /************************************************
                DuplexChannelFactory<MDC.IMyDuplexContract> dcf = new DuplexChannelFactory<MDC.IMyDuplexContract>(
                    context, 
                    new WSDualHttpBinding(),
                    "http://localhost:8733/Design_Time_Addresses/ZMHWcfServiceLibrary/MyDuplexContract/");
                MDC.IMyDuplexContract proxy = dcf.CreateChannel();
                proxy.DoWork();
                Console.ReadLine();
                dcf.Close();
                /************************************************
                PAS.IPublisherAndSubscriberCallback callback = new MySubscriber();
                InstanceContext context = new InstanceContext(callback);
                PAS.PublisherAndSubscriberClient proxy = new PAS.PublisherAndSubscriberClient(context);
                proxy.Subscribe(PAS.EventType.Event1);
                proxy.Subscribe(PAS.EventType.Event3);
                Console.ReadLine();
                proxy.DoSomething();
                Console.ReadLine();
                proxy.Close();
                /************************************************
                FESVC2.FaultExceptionSVCClient client = new FESVC2.FaultExceptionSVCClient();
                try
                {
                    //client.Divide(2, 0);
                    client.MethodWithError();
                    client.Close();
                }
                catch (FaultException<DivideByZeroException> exception)
                {
                    Console.WriteLine(exception.Message);
                }
                catch (FaultException<ExceptionDetail> exception)
                {
                    //Debug.Assert(exception.Detail.Type == typeof(InvalidOperationException).ToString());
                    //Debug.Assert(exception.Message == "Some error");
                    Console.WriteLine(exception.Message);
                }
                catch (CommunicationException exception)
                {
                    Console.WriteLine(exception.Message);
                }
             
                Console.ReadLine();
                /************************************************/

                /************************************************/
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
        }
    }
    class MySubscriber : PAS.IPublisherAndSubscriberCallback
    {
        public void OnEvent1()
        {
            Console.WriteLine("Event1");
        }

        public void OnEvent2(int number)
        {
            Console.WriteLine("Event2:{0}", number);
        }

        public void OnEvent3(int number, string text)
        {
            Console.WriteLine("Event3:{0}-{1}", number, text);
        }
    }
    class BackCall4Svc : MDC.IMyDuplexContractCallback
    {
        public string OnCallback()
        {
            return "hello boy!";
        }
    }
}
